<template>
  <a-modal
    title="编辑第三方对接"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          style="display: none;"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input v-decorator="['id']"/>
        </a-form-item>

        <a-form-item
          label="第三方接口地址"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入第三方接口地址" v-decorator="['thirdPartyUrl', {rules: [{required: true, message: '请输入第三方接口地址！'}]}]" />
        </a-form-item>

        <!--<a-form-item
          label="第三方标识"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入第三方标识" v-decorator="['appKey', {rules: [{required: true, message: '请输入第三方标识！'}]}]" />
        </a-form-item>-->

        <a-form-item
          label="业务系统名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback=""
        >
          <a-input placeholder="请输入业务系统名称" v-decorator="['businessSystemName', {rules: [{required: true, message: '请输入业务系统名称！'}]}]" />
        </a-form-item>

        <a-form :form="form">
          <a-form-item
            label="所属组织"
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            has-feedback=""
          >
            <a-tree-select
              v-decorator="['orgId', {rules: [{ required: true, message: '请选择机构！' }]}]"
              style="width: 100%"
              :dropdownStyle="{ maxHeight: '300px', overflow: 'auto' }"
              :treeData="orgTree"
              placeholder="请选择机构"
              treeDefaultExpandAll
              @change="e => initrOrgName(e)"
            >
              <span slot="title" slot-scope="{ id }">{{ id }}</span>
            </a-tree-select>
          </a-form-item>
          <a-form :form="form">
            <a-form-item v-show="false">
              <a-input v-decorator="['orgName']" />
            </a-form-item>
          </a-form>
        </a-form>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { edit } from '@/api/modular/main/thirdpartyinterconnection/thirdPartyInterconnectionManage'
  import { getOrgTree, getOrgList } from '@/api/modular/system/orgManage'

  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this),
        groupCodeList: [],
        orgTree: [],
        orgList: []
      }
    },
    methods: {
      edit (record) {
        this.visible = true
        this.getOrgDate()
        setTimeout(() => {
          this.form.setFieldsValue({
            id: record.id,
            thirdPartyUrl: record.thirdPartyUrl,
            // appKey: record.appKey,
            businessSystemName: record.businessSystemName,
            orgId: record.orgId
          })
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true

        validateFields((errors, values) => {
          if (!errors) {
            edit(values).then((res) => {
              this.confirmLoading = false
              if (res.success) {
                this.$message.success('编辑成功')
                this.handleCancel()
                this.$emit('ok', values)
              } else {
                this.$message.info('编辑失败：' + res.message)
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      },
      /**
       * 选择树机构，初始化机构名称于表单中
       */
      initrOrgName (value) {
        this.form.getFieldDecorator('orgName', { initialValue: this.orgList.find(item => value === item.id).name })
      },
      /**
       * 获取机构树，并加载于表单中
       */
      getOrgDate () {
        getOrgTree().then((res) => {
          this.orgTree = res.data
        })
        getOrgList().then((res) => {
          this.orgList = res.data
        })
      }
    }
  }
</script>
